package com.chengxusheji.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.chengxusheji.utils.ExportExcelUtil;
import com.chengxusheji.dao.MemberCardDAO;
import com.chengxusheji.domain.MemberCard;
import com.chengxusheji.dao.BusinessCompanyDAO;
import com.chengxusheji.domain.BusinessCompany;
import com.chengxusheji.dao.CardTypeDAO;
import com.chengxusheji.domain.CardType;
import com.chengxusheji.dao.CardStateDAO;
import com.chengxusheji.domain.CardState;
@Controller @Scope("prototype")
public class MemberCardAction extends ActionSupport {

    /*界面层需要查询的属性: 会员卡号*/
    private String cardNumber;
    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }
    public String getCardNumber() {
        return this.cardNumber;
    }

    /*界面层需要查询的属性: 所属商家*/
    private BusinessCompany companyObj;
    public void setCompanyObj(BusinessCompany companyObj) {
        this.companyObj = companyObj;
    }
    public BusinessCompany getCompanyObj() {
        return this.companyObj;
    }

    /*界面层需要查询的属性: 会员卡类别*/
    private CardType cardTypeObj;
    public void setCardTypeObj(CardType cardTypeObj) {
        this.cardTypeObj = cardTypeObj;
    }
    public CardType getCardTypeObj() {
        return this.cardTypeObj;
    }

    /*界面层需要查询的属性: 会员卡状态*/
    private CardState cardStateObj;
    public void setCardStateObj(CardState cardStateObj) {
        this.cardStateObj = cardStateObj;
    }
    public CardState getCardStateObj() {
        return this.cardStateObj;
    }

    /*当前第几页*/
    private int currentPage;
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getCurrentPage() {
        return currentPage;
    }

    /*一共多少页*/
    private int totalPage;
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

    /*当前查询的总记录数目*/
    private int recordNumber;
    public void setRecordNumber(int recordNumber) {
        this.recordNumber = recordNumber;
    }
    public int getRecordNumber() {
        return recordNumber;
    }

    /*业务层对象*/
    @Resource MemberCardDAO memberCardDAO;

    @Resource BusinessCompanyDAO businessCompanyDAO;
    @Resource CardTypeDAO cardTypeDAO;
    @Resource CardStateDAO cardStateDAO;
    /*待操作的MemberCard对象*/
    private MemberCard memberCard;
    public void setMemberCard(MemberCard memberCard) {
        this.memberCard = memberCard;
    }
    public MemberCard getMemberCard() {
        return this.memberCard;
    }

    /*跳转到添加MemberCard视图*/
    public String AddView() {
        ActionContext ctx = ActionContext.getContext();
        /*查询所有的BusinessCompany信息*/
        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfoCheckOk();
        ctx.put("businessCompanyList", businessCompanyList);
        /*查询所有的CardType信息*/
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        /*查询所有的CardState信息*/
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        return "add_view";
    }

    /*添加MemberCard信息*/
    @SuppressWarnings("deprecation")
    public String AddMemberCard() {
        ActionContext ctx = ActionContext.getContext();
        /*验证会员卡号是否已经存在*/
        String cardNumber = memberCard.getCardNumber();
        MemberCard db_memberCard = memberCardDAO.GetMemberCardByCardNumber(cardNumber);
        if(null != db_memberCard) {
            ctx.put("error",  java.net.URLEncoder.encode("该会员卡号已经存在!"));
            return "error";
        }
        try {
            
            BusinessCompany companyObj = businessCompanyDAO.GetBusinessCompanyBySjad(memberCard.getCompanyObj().getSjad());
            memberCard.setCompanyObj(companyObj);
           
            
            CardType cardTypeObj = cardTypeDAO.GetCardTypeByTypeId(memberCard.getCardTypeObj().getTypeId());
            memberCard.setCardTypeObj(cardTypeObj);
           
            
            CardState cardStateObj = cardStateDAO.GetCardStateByStateId(memberCard.getCardStateObj().getStateId());
            memberCard.setCardStateObj(cardStateObj);
           
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            memberCard.setAddTime(sdf.format(new java.util.Date()));
            
            memberCardDAO.AddMemberCard(memberCard);
            ctx.put("message",  java.net.URLEncoder.encode("MemberCard添加成功!"));
            return "add_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberCard添加失败!"));
            return "error";
        }
    }

    /*查询MemberCard信息*/
    public String QueryMemberCard() {
        if(currentPage == 0) currentPage = 1;
        if(cardNumber == null) cardNumber = "";
        List<MemberCard> memberCardList = memberCardDAO.QueryMemberCardInfo(cardNumber, companyObj, cardTypeObj, cardStateObj, currentPage);
        /*计算总的页数和总的记录数*/
        memberCardDAO.CalculateTotalPageAndRecordNumber(cardNumber, companyObj, cardTypeObj, cardStateObj);
        /*获取到总的页码数目*/
        totalPage = memberCardDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = memberCardDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("memberCardList",  memberCardList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("cardNumber", cardNumber);
        ctx.put("companyObj", companyObj);
        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfo();
        ctx.put("businessCompanyList", businessCompanyList);
        ctx.put("cardTypeObj", cardTypeObj);
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        ctx.put("cardStateObj", cardStateObj);
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        return "query_view";
    }
    
    

    /*查询MemberCard信息*/
    public String QueryMyMemberCard() {
    	ActionContext ctx = ActionContext.getContext();
        if(currentPage == 0) currentPage = 1;
        if(cardNumber == null) cardNumber = "";
        
        companyObj = new BusinessCompany();
        companyObj.setSjad((String)ctx.getSession().get("username"));
        
        List<MemberCard> memberCardList = memberCardDAO.QueryMemberCardInfo(cardNumber, companyObj, cardTypeObj, cardStateObj, currentPage);
        /*计算总的页数和总的记录数*/
        memberCardDAO.CalculateTotalPageAndRecordNumber(cardNumber, companyObj, cardTypeObj, cardStateObj);
        /*获取到总的页码数目*/
        totalPage = memberCardDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = memberCardDAO.getRecordNumber();
       
        ctx.put("memberCardList",  memberCardList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("cardNumber", cardNumber);
        ctx.put("companyObj", companyObj);
        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfo();
        ctx.put("businessCompanyList", businessCompanyList);
        ctx.put("cardTypeObj", cardTypeObj);
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        ctx.put("cardStateObj", cardStateObj);
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        return "company_query_view";
    }

    /*后台导出到excel*/
    public String QueryMemberCardOutputToExcel() { 
        if(cardNumber == null) cardNumber = "";
        List<MemberCard> memberCardList = memberCardDAO.QueryMemberCardInfo(cardNumber,companyObj,cardTypeObj,cardStateObj);
        ExportExcelUtil ex = new ExportExcelUtil();
        String title = "MemberCard信息记录"; 
        String[] headers = { "会员卡号","所属商家","会员卡类别","会员卡状态","会员卡余额","办理时间"};
        List<String[]> dataset = new ArrayList<String[]>(); 
        for(int i=0;i<memberCardList.size();i++) {
        	MemberCard memberCard = memberCardList.get(i); 
        	dataset.add(new String[]{memberCard.getCardNumber(),memberCard.getCompanyObj().getSjmc(),
memberCard.getCardTypeObj().getTypeName(),
memberCard.getCardStateObj().getStateName(),
memberCard.getMoney() + "",memberCard.getAddTime()});
        }
        /*
        OutputStream out = null;
		try {
			out = new FileOutputStream("C://output.xls");
			ex.exportExcel(title,headers, dataset, out);
		    out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		*/
		HttpServletResponse response = null;//创建一个HttpServletResponse对象 
		OutputStream out = null;//创建一个输出流对象 
		try { 
			response = ServletActionContext.getResponse();//初始化HttpServletResponse对象 
			out = response.getOutputStream();//
			response.setHeader("Content-disposition","attachment; filename="+"MemberCard.xls");//filename是下载的xls的名，建议最好用英文 
			response.setContentType("application/msexcel;charset=UTF-8");//设置类型 
			response.setHeader("Pragma","No-cache");//设置头 
			response.setHeader("Cache-Control","no-cache");//设置头 
			response.setDateHeader("Expires", 0);//设置日期头  
			String rootPath = ServletActionContext.getServletContext().getRealPath("/");
			ex.exportExcel(rootPath,title,headers, dataset, out);
			out.flush();
		} catch (IOException e) { 
			e.printStackTrace(); 
		}finally{
			try{
				if(out!=null){ 
					out.close(); 
				}
			}catch(IOException e){ 
				e.printStackTrace(); 
			} 
		}
		return null;
    }
    /*前台查询MemberCard信息*/
    public String FrontQueryMemberCard() {
        if(currentPage == 0) currentPage = 1;
        if(cardNumber == null) cardNumber = "";
        List<MemberCard> memberCardList = memberCardDAO.QueryMemberCardInfo(cardNumber, companyObj, cardTypeObj, cardStateObj, currentPage);
        /*计算总的页数和总的记录数*/
        memberCardDAO.CalculateTotalPageAndRecordNumber(cardNumber, companyObj, cardTypeObj, cardStateObj);
        /*获取到总的页码数目*/
        totalPage = memberCardDAO.getTotalPage();
        /*当前查询条件下总记录数*/
        recordNumber = memberCardDAO.getRecordNumber();
        ActionContext ctx = ActionContext.getContext();
        ctx.put("memberCardList",  memberCardList);
        ctx.put("totalPage", totalPage);
        ctx.put("recordNumber", recordNumber);
        ctx.put("currentPage", currentPage);
        ctx.put("cardNumber", cardNumber);
        ctx.put("companyObj", companyObj);
        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfo();
        ctx.put("businessCompanyList", businessCompanyList);
        ctx.put("cardTypeObj", cardTypeObj);
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        ctx.put("cardStateObj", cardStateObj);
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        return "front_query_view";
    }

    /*查询要修改的MemberCard信息*/
    public String ModifyMemberCardQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键cardNumber获取MemberCard对象*/
        MemberCard memberCard = memberCardDAO.GetMemberCardByCardNumber(cardNumber);

        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfo();
        ctx.put("businessCompanyList", businessCompanyList);
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        ctx.put("memberCard",  memberCard);
        return "modify_view";
    }

    /*查询要修改的MemberCard信息*/
    public String FrontShowMemberCardQuery() {
        ActionContext ctx = ActionContext.getContext();
        /*根据主键cardNumber获取MemberCard对象*/
        MemberCard memberCard = memberCardDAO.GetMemberCardByCardNumber(cardNumber);

        List<BusinessCompany> businessCompanyList = businessCompanyDAO.QueryAllBusinessCompanyInfo();
        ctx.put("businessCompanyList", businessCompanyList);
        List<CardType> cardTypeList = cardTypeDAO.QueryAllCardTypeInfo();
        ctx.put("cardTypeList", cardTypeList);
        List<CardState> cardStateList = cardStateDAO.QueryAllCardStateInfo();
        ctx.put("cardStateList", cardStateList);
        ctx.put("memberCard",  memberCard);
        return "front_show_view";
    }

    /*更新修改MemberCard信息*/
    public String ModifyMemberCard() {
        ActionContext ctx = ActionContext.getContext();
        try {
            if(true) {
            BusinessCompany companyObj = businessCompanyDAO.GetBusinessCompanyBySjad(memberCard.getCompanyObj().getSjad());
            memberCard.setCompanyObj(companyObj);
            }
            if(true) {
            CardType cardTypeObj = cardTypeDAO.GetCardTypeByTypeId(memberCard.getCardTypeObj().getTypeId());
            memberCard.setCardTypeObj(cardTypeObj);
            }
            if(true) {
            CardState cardStateObj = cardStateDAO.GetCardStateByStateId(memberCard.getCardStateObj().getStateId());
            memberCard.setCardStateObj(cardStateObj);
            }
            memberCardDAO.UpdateMemberCard(memberCard);
            ctx.put("message",  java.net.URLEncoder.encode("MemberCard信息更新成功!"));
            return "modify_success";
        } catch (Exception e) {
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberCard信息更新失败!"));
            return "error";
       }
   }

    /*删除MemberCard信息*/
    public String DeleteMemberCard() {
        ActionContext ctx = ActionContext.getContext();
        try { 
            memberCardDAO.DeleteMemberCard(cardNumber);
            ctx.put("message",  java.net.URLEncoder.encode("MemberCard删除成功!"));
            return "delete_success";
        } catch (Exception e) { 
            e.printStackTrace();
            ctx.put("error",  java.net.URLEncoder.encode("MemberCard删除失败!"));
            return "error";
        }
    }

}
